package cn.tedu.csmall.passport.web;

import cn.tedu.csmall.passport.ex.ServiceException;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

@Data
@Accessors(chain = true)
public class JsonResult<T> implements Serializable {

    private Integer state = ServiceCode.OK.getValue();
    private String message;

    private T data;

    public static JsonResult<Void> ok(){
        return JsonResult.ok(null);
    }

    public static <T> JsonResult<T> ok(T data){
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(data);
        return jsonResult;
    }

    public static JsonResult<Void> fail(ServiceException e){
        return JsonResult.fail(e.getServiceCode(),e.getMessage());
    }

    public static JsonResult<Void> fail(ServiceCode serviceCode,String message){
        JsonResult jsonResult = new JsonResult();
        jsonResult.setState(serviceCode.getValue()).setMessage(message);
        return jsonResult;
    }
}
